package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genesis.mall.entity.SmsFlashPromotionProductRelation;
import com.genesis.mall.entity.SmsFlashPromotionSession;
import com.genesis.mall.mapper.SmsFlashPromotionSessionMapper;
import com.genesis.mall.service.ISmsFlashPromotionProductRelationService;
import com.genesis.mall.service.ISmsFlashPromotionSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.vo.SmsFlashPromotionSessionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 限时购场次表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements ISmsFlashPromotionSessionService {
    @Autowired
    private ISmsFlashPromotionProductRelationService flashPromotionProductRelationService;

    /**
     * x修改状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        SmsFlashPromotionSession flashPromotionSession = this.getById(id);
        flashPromotionSession.setStatus(status);
        this.updateById(flashPromotionSession);
    }

    /**
     * 获取全部可选场次及其商品数量
     *
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<SmsFlashPromotionSessionVo> selectList(Integer flashPromotionId) {
        List<SmsFlashPromotionSessionVo> flashPromotionSessionVoList = new ArrayList<>();

        //在商品限时购与商品关系表根据flashPromotionId获取全部可选场次id
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> flashPromotionProductRelationQueryWrapper = new LambdaQueryWrapper<>();
        flashPromotionProductRelationQueryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        List<SmsFlashPromotionProductRelation> flashPromotionProductRelations = flashPromotionProductRelationService.list(flashPromotionProductRelationQueryWrapper);

        for (SmsFlashPromotionProductRelation flashPromotionProductRelation : flashPromotionProductRelations) {

            SmsFlashPromotionSessionVo flashPromotionSessionVo = new SmsFlashPromotionSessionVo();

            Long flashPromotionSessionId = flashPromotionProductRelation.getFlashPromotionSessionId();
            //查询启用场次表
            LambdaQueryWrapper<SmsFlashPromotionSession> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SmsFlashPromotionSession::getId, flashPromotionSessionId)
                    .eq(SmsFlashPromotionSession::getStatus, 1);
            SmsFlashPromotionSession flashPromotionSession = this.getOne(lqw);

            if (flashPromotionSession != null) {
                BeanUtils.copyProperties(flashPromotionSession, flashPromotionSessionVo);
                //封装商品数量
                flashPromotionSessionVo.setProductCount(flashPromotionProductRelation.getFlashPromotionCount());

                flashPromotionSessionVoList.add(flashPromotionSessionVo);
            }
        }
        return flashPromotionSessionVoList;
    }
}